package ynu.edu.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import ynu.edu.common.service.LogicService;
import ynu.edu.dao.*;
import ynu.edu.entity.*;


import javax.annotation.Resource;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;

@Service
public class CustomerService extends LogicService<CustomerDao, Customer,Long> {
    public CustomerService(@Autowired CustomerDao dao) {
        super(dao);
    }
    @Resource
    ShopItemDao shopItemDao;
    @Resource
    ShopDao shopDao;
    @Resource
    OrderDao orderDao;
    public List<Order1> getAllMyOrder(String name){
        Customer customer= getDAO().findCustomerByName(name);
        return orderDao.getAllMyOrderByCustomer(customer.getId());
    }
    public Order1 fundOrder(String name,Long orderId){
        Customer customer= getDAO().findCustomerByName(name);
        Order1 order=orderDao.getReferenceById(orderId);
        customer.fund(order);
        getDAO().save(customer);
        return order;
    }
    public Order1 cancelOrder(String name,Long orderId){
        Customer customer= getDAO().findCustomerByName(name);
        Order1 order=orderDao.getReferenceById(orderId);
        customer.cancelOrder(order);
        getDAO().save(customer);
        return order;
    }
    public Order1 findUnFundOrderByShop(String name,Long shopId){
        Customer customer= getDAO().findCustomerByName(name);
        Shop shop=shopDao.getReferenceById(shopId);
        List<Order1> orderList=customer.findOrderWithShop(shop);
        for(Order1 o:orderList){
            if(o.getStatus()==0){
                return o;
            }
        }
        return null;
    }
    public Order1 createOrder(String name,Long shopId,Long shopItemId,Integer amount){
        Order1 order=findUnFundOrderByShop(name,shopId);
        Customer customer = getDAO().findCustomerByName(name);
        Shop shop=shopDao.getReferenceById(shopId);
        LineItem lineItem = new LineItem();
        ShopItem shopItem = shopItemDao.getReferenceById(shopItemId);
        lineItem.setShopItem(shopItem);
        lineItem.setAmount(amount);
        if(order==null) {
            order = new Order1();
            order.setStatus(0);
            order.setCustomer(customer);
            order.setTotal(0.0);
            order.setShop(shop);
            order.setLineItems(new HashSet<>());
            customer.addOrder(order);
        }
        lineItem.setOrder1(order);
        order.addLineItem(lineItem);
        orderDao.save(order);
        return order;
    }
    public Customer register(String name, String password){
        Customer customer= getDAO().findCustomerByName(name);
        if(customer==null){
            customer=new Customer();
            customer.setName(name);
            customer.setPassword(password);
            customer.setUsername(name);
            getDAO().save(customer);
        }else customer=null;
        return customer;
    }
    public Customer login(String name,String password){
        Customer customer= getDAO().findCustomerByName(name);
        if(customer!=null){
            Customer customer1=new Customer();
            customer1.setPassword(password);

            if (!Objects.equals(customer1.getPassword(), customer.getPassword())){
                customer=null;
            }
        }
        return customer;
    }
}
